#!-*- encoding:utf8 -*-
from django.template import Template, Context, RequestContext
from django.shortcuts import render_to_response
from django.contrib.auth.models import User
from django.http import Http404, HttpResponseRedirect, HttpResponse
from newosmp.group.models import Group
from newosmp.util import commom
from newosmp import settings

def index(request):
    page = int(request.GET.get('page', '1'))
    group = Group.objects.all()
    num = settings.PAGE
    count = group.count()
    (group, page_list, page_num) = commom.pagination(group, page, num)
    (page_up, page_down) = commom.getPage(page, page_num) #上下页
    
    for g in group:
        user = g.user.split(',')
        g.user = []
        try:
            for i in user:
                g.user.append(User.objects.get(id = int(i)).username)
            g.user = ','.join(g.user)
        except:
            pass
    return render_to_response('group.html',locals())

def add(request):
    if request.method == 'POST':
        errors = []
        if not request.POST.get('name', ''):
            errors.append('请输入报警组名!')
        if not request.POST.getlist('uid'):
            errors.append('请选择小组成员!')
        if not request.POST.get('detail', ''):
            errors.append('请填写小组描述!')
        if errors:
            ulist = User.objects.all()
            return render_to_response('groupadd.html',locals())
        else:
            mode = request.POST.getlist('mode')
            code = ['0', '0', '0', '0']
            for e in mode:
                code[int(e)] = '1'
            mode = ''.join(code)
            
            user = ','.join(request.POST.getlist('uid'))
            
            group = Group.objects.create(
                        name = request.POST.get('name'),
                        user = user,
                        detail = request.POST.get('detail'),
                    )
            group.save()
            
            id = group.id
            return HttpResponseRedirect('/group')

    else:
        #无提交，转入模版，初始化数据
        ulist = User.objects.all()
        return render_to_response('groupadd.html', locals())

def delete(request):
    id = request.GET.get('id', '')
    if id:
        print id
        group = Group.objects.get(id = id)
        group.delete()
    return HttpResponseRedirect('/group')


def edit(request):
    if request.method == 'POST':
        errors = []
        if not request.POST.get('name', ''):
            errors.append('请输入报警组名!')
        if not request.POST.getlist('uid'):
            errors.append('请选择小组成员!')
        if not request.POST.get('detail', ''):
            errors.append('请填写小组描述!')
        if errors:
            ulist = User.objects.all()
            return render_to_response('groupedit.html',locals())
        else:
            id = int(request.POST.get("id"))
            group = Group.objects.get(id = id)
            group.name = request.POST.get('name')
            group.user = ','.join(request.POST.getlist('uid'))
            group.detail =request.POST.get('detail')
            group.save()
            return HttpResponseRedirect('/group')

    else:
        id = request.GET.get('id', '')
        group = Group.objects.get(id = id)
        check_lst =[int(e) for e in group.user.split(',')]
        ulist = User.objects.all()
    return render_to_response('groupedit.html', locals())


